package cn.lili.timetask.handler.impl.promotion;

import cn.lili.modules.goods.client.GoodsIndexClient;
import cn.lili.modules.promotion.client.SeckillClient;
import cn.lili.timetask.handler.EveryDayExecute;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 促销活动每日定时器
 *
 * @author Chopper
 * @since 2021/3/18 3:23 下午
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PromotionEverydayExecute implements EveryDayExecute {

    private final GoodsIndexClient goodsIndexClient;
    /**
     * 秒杀活动
     */
    private final SeckillClient seckillClient;

    /**
     * 将已过期的促销活动置为结束
     */
    @Override
    public void execute() {
        try {
            //清除所以商品索引的无效促销活动
            this.goodsIndexClient.cleanEsGoodsIndexInvalidPromotions();
        } catch (Exception e) {
            log.error("清楚商品索引中无效促销异常", e);
        }
        try {
            //定时创建活动
            addSeckill();
        } catch (Exception e) {
            log.error("秒杀活动添加异常", e);
        }

    }

    /**
     * 添加秒杀活动
     * 从系统设置中获取秒杀活动的配置
     * 添加30天后的秒杀活动
     */
    private void addSeckill() {
        seckillClient.generatorSeckill();
    }
}
